package com.mawc.equipower.controller;

import com.mawc.equipower.config.ContantConfig;
import com.mawc.equipower.mapper.ShowModeMapper;
import com.mawc.equipower.model.PaserMode;
import com.mawc.equipower.model.ShowModel;
import com.mawc.equipower.soket.SocketSendMsg;
import com.mawc.equipower.utils.CalculateData;
import com.mawc.equipower.utils.PaserExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName IndexController
 * @ProjectName equippower
 * @Description
 * @Author 马文才
 * @Date 2021.10.31 18:03
 */
@Controller
public class IndexController {

    @Autowired
    private ContantConfig contantConfig;

    @Autowired
    private ShowModeMapper showModeMapper;

    @GetMapping("/")
    public String index() {
        return "index.html";
    }

    @GetMapping("/showData")
    @ResponseBody
    public Map<String, List<ShowModel>> showModels() {
        List<ShowModel> data = getData();
        for (ShowModel datum : data) {
            showModeMapper.insert(datum);
        }

        Map<String, List<ShowModel>> collect = data.stream().collect(Collectors.groupingBy(ShowModel::getType));
        return collect;
    }

    /**
     * 获取数据
     */
    private List<ShowModel> getData() {
        String type = contantConfig.getType();
        if (type.equals("1")) {
            PaserExcel paserExcel = new PaserExcel(contantConfig.getFilepath(),contantConfig.getTestdata());
            List<PaserMode> collect = paserExcel.getPaserModes().stream().filter(it -> it.getFlag() != null && it.getFlag() == 1).collect(Collectors.toList());
            paserExcel.setPaserModes(collect);
            List<PaserMode> paserModes = paserExcel.translationData();
            List<ShowModel> showModels = CalculateData.showModels(paserModes);
            return showModels;
        }
        if (type.equals("2")) {

        }
        return null;
    }
    @Autowired
    private SocketSendMsg socketSendMsg;

    @PostMapping("/sendMsg")
    @ResponseBody
    public Map<String, Object> msg(String msg) {
        socketSendMsg.sendMsg();
        return null;
    }
}
